//
//  UpcomingDisplayDataCollector.m
//  ViperDemo
//
//  Created by Leeesven on 17/5/20.
//  Copyright © 2017年 Leeesven. All rights reserved.
//

#import "UpcomingDisplayDataCollector.h"

#import "UpcomingItem.h"
#import "UpcomingDisplayItem.h"
#import "UpcomingDisplaySection.h"
#import "UpcomingDisplayData.h"

#import "NSArray+HOM.h"

@interface UpcomingDisplayDataCollector ()

@property (nonatomic, strong)   NSDateFormatter*        dayFormatter;
@property (nonatomic, strong)   NSMutableDictionary*    sections;

@end

@implementation UpcomingDisplayDataCollector

- (void)addUpcomingItems:(NSArray *)upcomingItems{
    for (UpcomingItem *item in upcomingItems) {
        [self addUpcomingItem:item];
    }
}

- (void)addUpcomingItem:(UpcomingItem *)upcomingItem{
    UpcomingDisplayItem *displayItem = [self displayItemForUpcomingItem:upcomingItem];
    [self addDisplayItem:displayItem withDateRelation:upcomingItem.dateRelation];
}

- (UpcomingDisplayItem *)displayItemForUpcomingItem:(UpcomingItem *)upcomingItem
{
    NSString *formattedDay = [self formattedDay:upcomingItem.dueDate withDateRelation:upcomingItem.dateRelation];
    
    return [UpcomingDisplayItem upcomingDisplayItemWithTitle:upcomingItem.title dueDay:formattedDay];
}

- (NSString *)formattedDay:(NSDate *)date withDateRelation:(VTDNearTermDateRelation)dateRelation
{
    return [self needsFormattedDayForDateRelation:dateRelation] ? [self.dayFormatter stringFromDate:date] : @"";
}

- (BOOL)needsFormattedDayForDateRelation:(VTDNearTermDateRelation)dateRelation
{
    return (dateRelation != VTDNearTermDateRelationToday);
}

- (void)addDisplayItem:(UpcomingDisplayItem *)displayItem withDateRelation:(VTDNearTermDateRelation)dateRelation
{
    NSMutableArray *sectionItems = [self sectionItemsForDateRelation:dateRelation];
    [sectionItems addObject:displayItem];
}

- (NSMutableArray *)sectionItemsForDateRelation:(VTDNearTermDateRelation)dateRelation
{
    NSMutableDictionary *sections = self.sections;
    NSMutableArray *section = sections[@(dateRelation)];
    
    if (section == nil)
    {
        section = [NSMutableArray array];
        sections[@(dateRelation)] = section;
    }
    
    return section;
}

- (UpcomingDisplayData *)collectedDisplayData
{
    NSArray *displaySections = [[self collectedSectionKeys] arrayFromObjectsCollectedWithBlock:^id(NSNumber *sectionKey) {
        return [self displaySectionForKey:sectionKey];
    }];
    
    return [UpcomingDisplayData upcomingDisplayDataWithSections:displaySections];
}

- (NSArray *)collectedSectionKeys
{
    return [[self.sections allKeys] sortedArrayUsingSelector:@selector(compare:)];
}

- (UpcomingDisplaySection *)displaySectionForKey:(NSNumber *)sectionKey
{
    VTDNearTermDateRelation dateRelation = [sectionKey unsignedIntegerValue];
    NSString *sectionTitle = [self sectionTitleForDateRelation:dateRelation];
    NSString *imageName = [self sectionImageNameForDateRelation:dateRelation];
    
    return [UpcomingDisplaySection upcomingDisplaySectionWithName:sectionTitle
                                                           imageName:imageName
                                                               items:self.sections[sectionKey]];
}

- (NSString *)sectionImageNameForDateRelation:(VTDNearTermDateRelation)dateRelation
{
    switch (dateRelation)
    {
        case VTDNearTermDateRelationToday:
            return @"check";
            break;
            
        case VTDNearTermDateRelationTomorrow:
            return @"alarm";
            break;
            
        case VTDNearTermDateRelationLaterThisWeek:
            return @"circle";
            break;
            
        case VTDNearTermDateRelationNextWeek:
            return @"calendar";
            break;
            
        case VTDNearTermDateRelationOutOfRange:
            return @"paper";
            break;
    }
}


- (NSString *)sectionTitleForDateRelation:(VTDNearTermDateRelation)dateRelation
{
    switch (dateRelation)
    {
        case VTDNearTermDateRelationToday:
            return @"Today";
            break;
            
        case VTDNearTermDateRelationTomorrow:
            return @"Tomorrow";
            break;
            
        case VTDNearTermDateRelationLaterThisWeek:
            return @"Later This Week";
            break;
            
        case VTDNearTermDateRelationNextWeek:
            return @"Next Week";
            break;
            
        case VTDNearTermDateRelationOutOfRange:
            return @"Unknown";
            break;
    }
}

- (NSDateFormatter *)dayFormatter
{
    if (_dayFormatter == nil)
    {
        _dayFormatter = [[NSDateFormatter alloc] init];
        [_dayFormatter setDateFormat:[NSDateFormatter dateFormatFromTemplate:@"EEEE" options:0 locale:[NSLocale autoupdatingCurrentLocale]]];
    }
    
    return _dayFormatter;
}


- (NSMutableDictionary *)sections
{
    if (_sections == nil)
    {
        _sections = [NSMutableDictionary dictionary];
    }
    
    return _sections;
}


@end
